var http=require('http');
var https=require('https');
var qs=require('querystring');

exports.post=function(host,port,path,data,callback){
    var content = qs.stringify(data);
    var options={
        hostname:host,
        port:port,
        path:path+"?"+content,
        method:'GET'
    };

    var req=http.request(options,function(res){
        console.log('status:'+res.statusCode);
        console.log('headers:'+JSON.stringify(res.headers));
        res.setEncoding('utf8');
        res.on('data',function(chunk){
            callback(chunk);
        });
    });

    req.on('error',function(e){
        console.log('problem with request:'+e.message);
    });
    req.end();
};

exports.get2=function(url,data,callback,safe){
    var content=qs.stringify(data);
    var url=url+"?"+content;
    var proto=http;
    if(safe){
        proto=https;
    }
    var req=proto.get(url,function(res){
        res.setEncoding('utf8');
        res.on('data',function(chunk){
            var json=JSON.parse(chunk);
            callback(true,json);
        });
    });

    req.on('error',function(e){
        console.log('problem with request:'+e.message);
        callback(false,e);
    });
    req.end();
};

exports.get=function(host,port,path,data,callback,safe){
    var content=qs.stringify(data);
    var options={
        hostname:host,
        path:path+"?"+content,
        method:"GET"
    };
    if(port){
        options.port=port;
    }
    var proto=http;
    if(safe){
        proto=https;
    }
    var req=proto.request(options,function(res){
        res.setEncoding('utf8');
        res.on('data',function(chunk){
            var json=JSON.parse(chunk);
            callback(true,json);
        });
    });

    req.on('error',function(e){
        console.log('problem with request:'+e.message);
    });

    req.end();
};

exports.send=function(res,errcode,errmsg,data){
    if(data==null){
        data={};
    }
    data.errcode=errcode;
    data.errmsg=errmsg;
    var jsonstr=JSON.stringify(data);
    res.send(jsonstr);
};